//
//  SSApplicDetailViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/13.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSApplicDetailViewController.h"

#import "SSDetailTopViewCell.h"
#import "SSDetailScrollCell.h"
#import "SSDetailIntroduceCell.h"

#import "SSAppliDetailModel.h"
#import "SSCommentModel.h"
#import "SSCommentsView.h"
#import "SSCommentCell.h"

#import "NSString+Size.h"
@interface SSApplicDetailViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *topDetailData;
@property (nonatomic,strong) SSCommentsView *commentView;
@property (nonatomic,strong) NSMutableArray *commentData;
@end

static NSString *const detailTopViewCellID = @"detailTopViewCellID";
static NSString *const detailScrollCellID = @"detailScrollCellID";
static NSString *const detailIntroduceCellID = @"detailIntroduceCellID";
static NSString *const detailCommentCellID = @"detailCommentCellID";

@implementation SSApplicDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"应用详情";
    
    [self requestTopDetailData];
    [self setupTableView];
    [self requestCommentData];
    [self setupCommentView];
}

- (void)requestTopDetailData {
   
    NSDictionary *param = @{@"id":self.guid,@"platform":@(3)};
     kWeakSelf(self, weakSelf);
    [self showHUD];
    [SSNetworkManager getWithURLString:kSSHandPickListAPI parameters:param success:^(NSDictionary *responseDict) {
        SSAppliDetailModel *detailModel = [SSAppliDetailModel mj_objectWithKeyValues:responseDict[@"data"]];
        [self.topDetailData addObject:detailModel];
       
        [weakSelf.tableView reloadData];
        [self dismissHUD];
    } failure:^(NSError *error) {
         [self dismissHUD];
    }];

}

- (void)requestCommentData {
    
    NSDictionary *param = @{@"articleId":self.guid,@"pageIndex":@(0),@"pageSize":@(10)};
    
    [SSNetworkManager getWithURLString:kSSHandCommentListAPI parameters:param success:^(NSDictionary *responseDict) {
      

    for (NSDictionary *dict in responseDict[@"data"]) {
            SSCommentModel *model = [SSCommentModel mj_objectWithKeyValues:dict];
            [self.commentData addObject:model];
        }

        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
       
    }];

}


- (void) setupTableView{
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.bottom.mas_offset(-44);
    }];
}

- (void)setupCommentView {
    
    self.commentView = [[SSCommentsView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.commentView];
    [self.commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SSDetailTopViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailTopViewCellID forIndexPath:indexPath];
        cell.model = self.topDetailData.firstObject;
        kWeakSelf(self, weakSelf);
        cell.clickBtn = ^(NSString *downloadPath){
                [weakSelf showTip:downloadPath];
        };
        return cell;
    }else if(indexPath.section == 1){
        SSDetailScrollCell *cell = [tableView dequeueReusableCellWithIdentifier:detailScrollCellID];
        cell.model = self.topDetailData.firstObject;
        return cell;
        
    }else if(indexPath.section == 2){
        SSDetailIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:detailIntroduceCellID];
        cell.model = self.topDetailData.firstObject;
        return cell;
        
    }else {
        SSCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:detailCommentCellID];
        cell.dataSource = self.commentData;
        return cell;
    
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height;
    switch (indexPath.section) {
        case 0:
            height = 150;
            break;
        case 1:
            height = 180;
            break;
        case 2:
            {
                SSAppliDetailModel *model = self.topDetailData.firstObject;
                height = model.cellHeight;
            
            }
            break;
        case 3:
            {
                CGFloat cellH;
                CGFloat contentW = kScreenWidth - 105;
                for (SSCommentModel *model in self.commentData) {
                    
                    CGFloat contentH = [model.content heightWithFont:kFont(15) constrainedToWidth:contentW];
                    if (contentH < 20) {
                        contentH = 20;
                    }
                    CGFloat H = contentH + 82;
                    cellH += H;
                    
                    ;
                }
                height = cellH;
            }
            
            break;
        default:
            break;
    }
    return height;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate= self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"SSDetailTopViewCell" bundle:nil] forCellReuseIdentifier:detailTopViewCellID];
        [_tableView registerClass:[SSDetailScrollCell class] forCellReuseIdentifier:detailScrollCellID];
        [_tableView registerClass:[SSDetailIntroduceCell class] forCellReuseIdentifier:detailIntroduceCellID];
        [_tableView registerClass:[SSCommentCell class] forCellReuseIdentifier:detailCommentCellID];
    }
    return _tableView;
}

- (NSMutableArray *)topDetailData {
    if (!_topDetailData) {
        _topDetailData = [NSMutableArray array];
    }
    return _topDetailData;
}

- (NSMutableArray *)commentData {
    if (!_commentData) {
        _commentData = [NSMutableArray array];
    }
    return _commentData;
}
@end
